#!/usr/bin/env python3.9
# argv has a raw memory dump
# needs a cmd for setting the screen blackground colour and character set

import time,serial,sys

f = open(sys.argv[1],"rb")
memory = f.read()

step = 40
out = "ZZZ:040000"

def addstring (a, s):
    global out
    o = ":{:04X}".format(a)
    for i in range(len(s)):
        c = ord(s[i])
        if c >= 64 and c <= 95: c -= 64
        o += "{:02X}".format(c)
    out += o

addstring(2064, "ORIGINAL BY TOMOALIEN")
addstring(2104, "FROM HTTPS://CSDB.DK/RELEASE/?ID=158097") # solus.dump
addstring(2184, "GENERATED FROM CHARACTERS (NOT PIXELS)  ")
addstring(2224, "AND TRANSMITTED BY AN ARDUINO")
addstring(2297, "VK2CJB")

for addr in list(range(1024,2024,step)) + list(range(55296,56296,step)):
    o = ":" + "{:04X}".format(addr)
    for i in range(addr,addr+step):
        o += "{:02X}".format(memory[i])
    out += o

out = bytes(out+"TZ\n","UTF-8")

ser = serial.Serial("/dev/ttyACM0", 9600)
ser.readline()                  # waits for "START"
#ser.write(out)
for i in out.split(b':'):
    ser.write(b":"+i)
    print (i)
    time.sleep(0.01)

time.sleep(5)
ser.readline()                  # waits for "END"
ser.close()
